/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.content {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.data.TabsText;
	import com.googlecode.milib.data.value.MLNumberValue;
	import com.googlecode.milib.dbg.MLDBG;
	import com.googlecode.milib.dbg.model.content.MLDBGValuesListModel;
	import com.googlecode.milib.service.singleton.LinksFromMLTextFieldGlobalBroadcaster;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.events.TextEvent;
	import flash.system.System;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGDynamicNumbersList extends MLDBGValuesListModel implements IMLBroadcasterListener {

		private static var instance:MLDBGDynamicNumbersList;
		private var numbers:Array;
		private var links:LinksFromMLTextFieldGlobalBroadcaster;

		public function MLDBGDynamicNumbersList(access:Private) {
			super(false);
			numbers=[];
			addValue('<U><A HREF="event:dynamicNumberPaste">Paste numbers create AS to clippboard</A></U>', new MLNumberValue());
			links=LinksFromMLTextFieldGlobalBroadcaster.getInstance();
			links.addListener(this);
		}
		
		public function regDynamicNumber(num:MLNumberValue):void {
			addValue(MLDBG.getInstance().getValueLinked(num, num.getID()), num);
			numbers.push(num);
		}
		
		public function getNumbers():Array {
			return numbers.concat();
		}
		
		private function pasteDynamicNumbersDataToClippboard():void {
			var classStr:TabsText=new TabsText();
			classStr.plusTab();
			classStr.plusTab();
			var i:uint,dn:MLNumberValue;
			for(i=0;i<numbers.length;i++){
				dn=numbers[i];
				classStr.addLine('public static var dynamicNumber_'+dn.getID()+' : MLNumberValue;');
			}
			classStr.addLine('');
			classStr.addLineAndPlus('public static function setupDynamicNumbers():void {');
			for(i=0;i<numbers.length;i++){
				dn=numbers[i];
				classStr.addLine('MLNumberValue.getInstanceForID("'+dn.getID()+'").v='+dn.v+';');
			}
			classStr.minusAndAddLine('}');
			
			System.setClipboard(classStr.toString());
		}
		
		public static function getInstance():MLDBGDynamicNumbersList {
			if(instance) { return instance; }
			else{ instance=new MLDBGDynamicNumbersList(null); return instance; }
		}
		
		public static var dynamicNumber_ToA : MLNumberValue;
		public static var dynamicNumber_ToB : MLNumberValue;
		
		public static function setupDynamicNumbers():void {
			dynamicNumber_ToA=MLNumberValue.getInstanceForID("ToA");
			dynamicNumber_ToA.v=66;
			dynamicNumber_ToB=MLNumberValue.getInstanceForID("ToB");
			dynamicNumber_ToB.v=7778;
		}
		
//********************************************************************************************
//		events for MLDBGDynamicNumbersList
//********************************************************************************************
		override public function onEvent(ev:MLEventInfo):void {
			super.onEvent(ev);
			switch(ev.hero){
				case links:
					switch(ev.event) {
						case links.event_Link:
							if(TextEvent(ev.data).text=='dynamicNumberPaste'){
								pasteDynamicNumbersDataToClippboard();
							}
						break;
					}
				break;
			}
		}
		
	}
}
internal class Private {}